package pathfind;

import data.NavmeshRegion;
import data.FRectangle;
import data.IndexPair;
import data.Params;
import data.Triangle;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Dmitry
 */
public class PolygonProcessor {

    public List<IndexPair> getPolygonOutline(List<Triangle> triangles) {
        Set<IndexPair> outlinePairs = new HashSet<>();
        for (Triangle triangle : triangles) {
            addIndex(new IndexPair(triangle.getIndex1(), triangle.getIndex2()), outlinePairs);
            addIndex(new IndexPair(triangle.getIndex1(), triangle.getIndex3()), outlinePairs);
            addIndex(new IndexPair(triangle.getIndex2(), triangle.getIndex3()), outlinePairs);
        }

        return new ArrayList<>(outlinePairs);
    }

    private void addIndex(IndexPair pair, Set<IndexPair> outlinePairs) {
        if (outlinePairs.contains(pair)) {
            outlinePairs.remove(pair);
            return;
        }

        outlinePairs.add(pair);
    }

    public void processGrid(NavmeshRegion polygon) {
        FRectangle bounds = polygon.getBoundaries();
        double minx = bounds.getMinX();
        double maxx = bounds.getMaxX();
        double miny = bounds.getMinY();
        double maxy = bounds.getMaxY();
        minx = ((int) (minx / Params.CELL_SIZE)) * Params.CELL_SIZE;
        maxx = ((int) (maxx / Params.CELL_SIZE) + 1) * Params.CELL_SIZE;
        miny = ((int) (miny / Params.CELL_SIZE)) * Params.CELL_SIZE;
        maxy = ((int) (maxy / Params.CELL_SIZE) + 1) * Params.CELL_SIZE;
        int linesCount = (int) Math.round((maxy - miny) / Params.CELL_SIZE);
    }
}
